﻿//------------------------------------------------------------
// Author: 烟雨迷离半世殇
// Mail: 1778139321@qq.com
// Data: 2020年1月5日 22:01:23
//------------------------------------------------------------

using System;


namespace NKG_MVVM_UI
{
    public class StateFactory
    {
        /// <summary>
        /// 自动根据新值,以及新值得属性名来生成一个新的State
        /// </summary>
        public static void CreateState<T, A>(string newestPropertyInfoName, A a) where T : AState
        {
            T oldState2newState;
            if (StateManager.Instance.AllStates.TryGetValue(typeof(T), out var queue))
            {
                AState[] aStatesArrays = queue.ToArray();
                oldState2newState = ((T)aStatesArrays[aStatesArrays.Length-1]).ShadowClone<T>();
                oldState2newState.PropertyInfos[newestPropertyInfoName].SetValue(oldState2newState, a);
            }
            else
            {
                oldState2newState = Activator.CreateInstance<T>();
                oldState2newState.PropertyInfos[newestPropertyInfoName].SetValue(oldState2newState, a);
            }

            StateManager.Instance.AddState(oldState2newState);
        }
    }
}